﻿using HLStock.DBAccess;
using HLStock.DBAccess.AOP;
using HLStock.Model;
using HLStock.ModelSetup;
using HLStock.MyExcep;

namespace HLStock.WmsService
{
    public class StockRecordServiceProvider : BaseServiceProvider<StockRecordModel>
    {
        public StockRecordServiceProvider() 
        {
            AddDefValSetter(new StockInOrderSetup());
            AddDefValSetter(new StockInPlaceSetup());
            AddDefValSetter(new StockOutSetup());
        }
        public override DbUpdater<StockRecordModel> CreateUpdateInstance()
        {
            var instance = base.CreateUpdateInstance();
            instance.RegBeforeSaveHandlers(new StockOutSourceCheckAction());
            instance.RegBeforeSaveHandlers(new StockOutLimitCheckAction());
            instance.RegAfterSaveHandlers(new StockOutRecordRewrite());
            instance.RegAfterDeleteHandlers(new StockOutRecordRewrite());
            instance.RegAfterDeleteHandlers(new StockInClearOnDeleteAction());

            return instance;
        }
        public void DeleteAllOut(string srcrecord)
        {
            var list = ListOutRecord(srcrecord);
            var upd = new DbUpdater<StockRecordModel>(instance);
            foreach (var item in list)
            {
                upd.Delete(item.Pk);
            }
        }
        public List<StockRecordModel> ListOutRecord(string srcrecord)
        {
            var cond = CondCollection.True()
                .Eq(nameof(StockRecordModel.Type), (int)StockType.Out)
                .Eq(nameof(StockRecordModel.SrcRecord), srcrecord);
            return List(cond);
        }
        public List<StockRecordModel> SearchByKeyword(string keyword)
        {
            if (String.IsNullOrWhiteSpace(keyword)) return List(CondCollection.True()
                .Eq(nameof(StockRecordModel.Type), (int)StockType.In)
                .NotEq(nameof(StockRecordModel.OnHand), 0));
            keyword = "%" + keyword + "%";
            var cond1 = CondCollection.True()
                .Eq(nameof(StockRecordModel.Type),(int)StockType.In)
                .Lk(nameof(StockRecordModel.OrderCode), keyword)
                .NotEq(nameof(StockRecordModel.OnHand),0);
            var cond2 = CondCollection.True()
                .Eq(nameof(StockRecordModel.Type), (int)StockType.In)
                .Lk(nameof(StockRecordModel.QrCode), keyword)
                .NotEq(nameof(StockRecordModel.OnHand), 0);
            var cond3 = CondCollection.True()
                .Eq(nameof(StockRecordModel.Type), (int)StockType.In)
                .Lk(nameof(StockRecordModel.ProductName), keyword)
                .NotEq(nameof(StockRecordModel.OnHand), 0);
            var cond4 = CondCollection.True()
                .Eq(nameof(StockRecordModel.Type), (int)StockType.In)
                .Lk(nameof(StockRecordModel.PlaceCode), keyword)
                .NotEq(nameof(StockRecordModel.OnHand), 0);
            var v1 = List(cond1);
            var v2 = List(cond2);
            var v3 = List(cond3);
            var v4 = List(cond4);
            var v = v1.Union(v2).Union(v3).Union(v4).Distinct(new ModelEqualityComparer<StockRecordModel>());
            return v.ToList();
        }
        public void UpdateOnHand(StockRecordModel model)
        {
            if (model.Type == StockType.Out) throw new MyDataOptException("不支持为出库单回写数量");
            var allOutRecords = ListOutRecord(model.Pk);
            var outQtySum = allOutRecords.Sum(r => r.Qty);
            model.OnHand = model.Qty - outQtySum;
            Save(model);
        }
    }
}
